package com.rest.demo.Service;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.rest.demo.util.Md5;
import com.rest.demo.util.Sha256Util;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;

import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;


import java.util.Date;

public class DysmtIDCard {
    RestTemplate restTemplate = new RestTemplate();
    //带请求头的head
    public JSONObject GetCard(String sfzh ,String appSecret,String AppKey) {

        Long timestamp=System.currentTimeMillis()/1000;
        String StrTime =timestamp.toString();
        String nonce = RandomUtil.randomNumbers(6);

        String cardHash=Sha256Util.getSHA256(sfzh);

        String parameter = "cardHash="+cardHash+"&nonce="+nonce+"&timestamp="+timestamp+appSecret;
        System.out.println(parameter);
        String sign = Md5.MD5Small(parameter);
//        String url = "https://t-dywz.ikanke.cn/app/api/person/qr_get";
        String url = "https://dy.ikanke.cn/app/api/person/qr_get";
        HttpHeaders headers = new HttpHeaders();
        headers.add("App-Key", AppKey);//请求头内放入APPKEY

        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("cardHash", cardHash);
        map.add("timestamp", StrTime);
        map.add("nonce", nonce);
        map.add("sign", sign);
        System.out.println("map"+map);
System.out.println("sign"+sign);
        // 组装请求体
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);


        // 发送post请求，并打印结果，以String类型接收响应结果JSON字符串
        String result = restTemplate.postForObject(url, request, String.class);
        System.out.println(result);

        System.err.println(url);



        JSONObject data = JSONObject.parseObject(result);
        System.out.println(data);
//        JSONObject UserHealth = JSONObject.parseObject(data).getJSONObject("data");//




        return data;

    }




        //postForObject模拟表单数据提交
//    提交x-www-form-urlencoded格式的数据
    public String postForObjectForm() {

        // 请求地址
        String url = "http://auths-test.dysmt.cn/oauth/token";


        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("code", "TRb1Dh");
        map.add("client_id", "057ffa4052294d45af9b9bac69dc4c8d");
        map.add("client_secret", "2faa6ccb0ca94bfc84222a8c7aa31c5c");
        map.add("grant_type", "authorization_code");
        map.add("redirect_uri", "https://127.0.0.1/");

        // 组装请求体
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);

        // 发送post请求，并打印结果，以String类型接收响应结果JSON字符串
        String result = restTemplate.postForObject(url, request, String.class);
        System.out.println(result);
        return result;
    }


}
